# -*- coding:utf-8 -*-
from django.db import models


class IDC(models.Model):
    name = models.CharField(u"机房名称", max_length=64)
    description = models.TextField(u"具体描述", default=u"请详细描述该机房的其他细节")
    contact = models.CharField(u"联系人", max_length=32)
    # mail = models.EmailField(u"邮箱")
    telphone = models.CharField(u"电话", max_length=32)
    dongdong = models.CharField(u"咚咚", max_length=32)
    address = models.CharField(u"地址", max_length=128)
    customer_id = models.CharField(u"服务对象", max_length=128, blank=True, null=True)

    create_time = models.DateField(u'创建时间', auto_now=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u"机房"
        verbose_name_plural = verbose_name


SERVER_STATUS = (
    (0, u"正常"),
    (1, u"宕机"),
    (2, u"未连接"),
    (3, u"错误"),
)
SERVICE_TYPES = (
    ('dhcp', u"DHCP"),
    ('moniter', u"Moniter"),
    ('lvs', u"LVS"),
    ('db', u"数据库"),
    ('analysis', u"分析"),
    ('admin', u"管理"),
    ('storge', u"存储"),
    ('web', u"网站"),
    ('email', u"邮箱"),
    ('mix', u"Mix"),
)

'''
定义服务器
'''


class Server(models.Model):
    idc = models.ForeignKey(IDC, verbose_name=u'机房')
    name = models.CharField(u'服务器名称', max_length=64)
    nagios_name = models.CharField(u"Nagios主机ID", max_length=64, blank=True, null=True)
    ip = models.IPAddressField(u'管理IP')
    internal_ip = models.IPAddressField(u'内部IP')
    user = models.CharField(u"账号", max_length=64)
    password = models.CharField(u"密码", max_length=128)
    ssh_port = models.IntegerField(u"端口", blank=True, null=True)
    status = models.SmallIntegerField(u"状态", choices=SERVER_STATUS)

    brand = models.CharField(u"品牌", max_length=64, choices=[(i, i) for i in (u"IBM", u"DELL", u"HP", u"Other")])
    model = models.CharField(u'型号', max_length=64)
    cpu = models.CharField(u"处理器", max_length=64)
    core_num = models.SmallIntegerField(u"内核", choices=[(i * 2, "%s Cores" % (i * 2)) for i in range(1, 15)])
    hard_disk = models.IntegerField(u"硬盘")
    memory = models.IntegerField(u"内存")

    system = models.CharField(u"系统", max_length=32,
                              choices=[(i, i) for i in (u"Windows", u"CentOS", u"FreeBSD", u"Ubuntu")])
    system_version = models.CharField(u'系统版本', max_length=32)
    system_arch = models.CharField(u"系统架构", max_length=32, choices=[(i, i) for i in (u"x86_64", u"i386")])

    create_time = models.DateField(u"创建时间")
    guarantee_date = models.DateField(u'保修时间')
    service_type = models.CharField(u"服务器类型", max_length=32, choices=SERVICE_TYPES)
    description = models.TextField(u"其他描述")

    @property
    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u"服务器"
        verbose_name_plural = verbose_name


SWITCH_QUYU = (
    (0, "华北"),
    (1, "其他"),
)

SWITCH_CITY = (
    (0, "北京"),
    (1, "青岛"),
    (2, "西安"),
    (3, "呼和浩特"),
    (4, "乌鲁木齐"),
    (5, "张家口"),
)

SWITCH_PINPAI = (
    (0, "思科"),
    (1, "华为"),
    (2, "H3C"),
)

'''
定义交换机
'''


class Switchs(models.Model):
    quyu = models.SmallIntegerField(u"区域", choices=SWITCH_QUYU)
    city = models.SmallIntegerField(u"城市", choices=SWITCH_CITY)
    Room = models.ForeignKey(IDC, verbose_name=u'所属机房')
    weizhi = models.CharField(u"位置", max_length=32)
    pinpai = models.SmallIntegerField(u"品牌", choices=SWITCH_PINPAI)
    mingcheng = models.CharField(u"型号", max_length=32)
    sn = models.CharField(u"SN", max_length=32)
    xzcode = models.CharField(u"行政固资编号", max_length=32, blank=True, null=True,
                              help_text=u"DJ-03开头的固资编码")
    ywcode = models.CharField(u"运维固资编号", max_length=32, blank=True, null=True,
                              help_text=u"ZB201X开头的固资编码")
    usetime = models.DateField(u"使用时间")
    ip = models.IPAddressField(u"管理IP", blank=True, null=True)
    beizhu = models.TextField(u"备注说明", blank=True, null=True)

    class Meta:
        verbose_name = u"交换机"
        verbose_name_plural = verbose_name


'''
定义交换机组
'''


class SwitchGroup(models.Model):
    name = models.CharField(u'名称', max_length=32)
    description = models.TextField(u'详细说明', max_length=512)
    switch = models.ManyToManyField(Switchs, verbose_name=u'交换机', blank=True, related_name='groups')

    class Meta:
        verbose_name = u"交换机组"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.name


class ServerGroup(models.Model):
    name = models.CharField(u'名称', max_length=32)
    description = models.TextField(u'详细说明')
    hosts = models.ManyToManyField(Server, verbose_name=u'服务器', blank=True, related_name='groups')

    class Meta:
        verbose_name = u"服务器组"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.name


'''
定义防火墙
'''


class Firewalls(models.Model):
    Room = models.ForeignKey(IDC, verbose_name=u'所属机房')
    name = models.CharField(u'名称', max_length=32)
    SID = models.CharField(u'SID', max_length=64)
    code = models.CharField(u"资产编号", max_length=32, blank=True, null=True, )
    zgxip = models.IPAddressField(u"主光纤地址", blank=True, null=True)
    bgxip = models.IPAddressField(u"备光纤地址", blank=True, null=True)
    Roomdata = models.DateField(u'建仓日期')
    Location = models.CharField(u'位置', max_length=64,
                                help_text=u"机柜U位")
    beizhu = models.TextField(u"备注说明", blank=True, null=True)

    class Meta:
        verbose_name = u"防火墙"
        verbose_name_plural = verbose_name


'''
定义防火墙
'''
UPS_BX = (
    (0, "是"),
    (1, "否"),
    (2, "未知"),
)


class Upss(models.Model):
    syweizhi = models.CharField(u'使用地方', max_length=64,
                                help_text=u'地址 例如北京市通州区光机电产业一体化基地科创东5街8号')
    kufangname = models.CharField(u'仓库名称', max_length=32)
    xinghao = models.CharField(u'UPS型号', max_length=64)
    gonglv = models.CharField(u"功率大小", max_length=16, blank=True, null=True,
                              help_text=u'例如:20KW')
    dcsl = models.CharField(u"电池数量", max_length=6, blank=True, null=True)
    aztime = models.DateField(u"安装时间")
    SN = models.CharField(u'SN', max_length=32)
    zccode = models.CharField(u'资产编码', max_length=64)
    baoxiu = models.SmallIntegerField(u"是否在保", choices=UPS_BX)
    lxr = models.CharField(u'联系人', max_length=16)
    lxfs = models.CharField(u"联系方式", max_length=11,
                            help_text=u"手机号.限制长度11位")
    lxdz = models.CharField(u"联系地址", max_length=128)
    beizhu = models.TextField(u"备注", blank=True, null=True)

    class Meta:
        verbose_name = u"UPS"
        verbose_name_plural = verbose_name


'''
定义宽带数据库
Broadband表
'''


class Broadbands(models.Model):
    kufang = models.CharField(u'库房', max_length=64)
    xinghao = models.CharField(u'设备型号', max_length=64)
    isp = models.CharField(u'运营商', max_length=64)
    wwip = models.IPAddressField(u'外网地址')
    gw = models.CharField(u'网关', max_length=64)
    nwwd = models.CharField(u'内网网段', max_length=64)
    daikuan = models.CharField(u'带宽', max_length=64)
    osver = models.CharField(u'系统版本', max_length=64,
                             help_text=u"未知,明天改")
    beizhu = models.TextField(u"备注", blank=True, null=True)

    class Meta:
        verbose_name = u"宽带"
        verbose_name_plural = verbose_name


'''

定义 光纤 数据库
Fiber表
'''


class Fibers(models.Model):
    quyu = models.CharField(u'区域', max_length=32)
    kuqu = models.CharField(u'库区', max_length=128)
    isp = models.CharField(u'运营商', max_length=64)
    gxhao = models.CharField(u'光纤号(报障号)', max_length=64)
    kehujl = models.CharField(u'客户经理', max_length=16)
    phone = models.CharField(u'电话', max_length=64)

    class Meta:
        verbose_name = u"光纤"
        verbose_name_plural = verbose_name


'''
AP
APs表
'''


class APs(models.Model):
    szdd = models.CharField(u'所在地点', max_length=32)
    xinghao = models.CharField(u'型号', max_length=64)
    sn = models.CharField(u'SN', max_length=32)
    gzbh = models.CharField(u'固资编号', max_length=16)
    mac = models.CharField(u'MAC地址', max_length=18)
    lanip = models.IPAddressField(u"管理地址(LAN)", blank=True, null=True)
    vanip = models.IPAddressField(u"管理地址(WAN)", blank=True, null=True)
    loginname = models.CharField(u'登录名', max_length=16)
    loginpasswd = models.CharField(u'登陆密码', max_length=32)
    wifiname = models.CharField(u'无线名', max_length=16)
    wifipasswd = models.CharField(u'无线密码', max_length=32)

    class Meta:
        verbose_name = u"无线AP"
        verbose_name_plural = verbose_name


'''
AC
ACs表
'''

AC_STATUS = (
    (0, u"使用"),
    (1, u"备用"),
    (2, u"闲置"),
)


class ACs(models.Model):
    quyu = models.SmallIntegerField(u"区域", choices=SWITCH_QUYU)
    cangbie = models.CharField(u'型号', max_length=64)
    zhuangtai = models.SmallIntegerField(u"状态", choices=AC_STATUS)
    ac = models.CharField(u'AC', max_length=16,
                          help_text=u'AC型号')
    ap = models.CharField(u'AP', max_length=18,
                          help_text=u'AP数量')
    apxh = models.CharField(u"AP型号", max_length=64)
    beizhu = models.TextField(u"备注", blank=True, null=True)

    class Meta:
        verbose_name = u"AC统计"
        verbose_name_plural = verbose_name


class MaintainLog(models.Model):
    host = models.ForeignKey(Server, verbose_name=u'服务器', blank=True, null=True)
    switch = models.ForeignKey(Switchs, verbose_name=u'交换机', blank=True, null=True)
    maintain_type = models.CharField(u"维护类型", max_length=32)
    hard_type = models.CharField(u'硬件类型', max_length=16)
    time = models.DateTimeField(u"操作时间", )
    operator = models.CharField(u"操作人", max_length=16)
    note = models.TextField(u'详细记录', default=u"请输入具体步骤")

    @property
    def __unicode__(self):
        return u'%s 维护日志 [%s] %s %s' % (
            self.host.name, self.time.strftime('%Y-%m-%d %H:%M:%S'), self.maintain_type, self.hard_type)

    class Meta:
        verbose_name = u"维护日志"
        verbose_name_plural = verbose_name


class AccessRecord(models.Model):
    date = models.DateField()
    user_count = models.IntegerField()
    view_count = models.IntegerField()

    class Meta:
        verbose_name = u"访问记录"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return u"%s 访问记录" % self.date.strftime('%Y-%m-%d')
